<?php

namespace app\supplier\model\order;

use app\common\model\order\OrderRefund as OrderRefundModel;
use app\common\model\order\Order as OrderModel;
/**
 * 售后管理模型
 */
class OrderRefund extends OrderRefundModel
{
    /**
     * 获取售后单列表
     */
    public function getListBlack($query = [], $shop_supplier_id = 0)
    {

        $model = $this;
        // 查询条件：订单号
        if (isset($query['order_no']) && !empty($query['order_no'])) {
            $model = $model->where('order.order_no', 'like', "%{$query['order_no']}%");
        }
        // 查询条件：起始日期
        if (isset($query['create_time']) && !empty($query['create_time'])) {
            $sta_time = array_shift($query['create_time']);
            $end_time = array_pop($query['create_time']);
            $model = $model->whereBetweenTime('m.create_time', $sta_time, date('Y-m-d 23:59:59', strtotime($end_time)));
        }
        // 售后类型
        if (isset($query['type']) && $query['type'] > 0) {
            $model = $model->where('m.type', '=', $query['type']);
        }

        // 售后单状态(选项卡)
        if (isset($query['status']) && $query['status'] >= 0) {
            $model = $model->where('m.status', '=', $query['status']);
        }
        if ($shop_supplier_id) {
            $model = $model->where('m.shop_supplier_id', '=', $shop_supplier_id);
        }

        // 获取列表数据
        $list = $model->alias('m')
            ->field('m.*, order.order_no')
            ->with(['orderproduct.image', 'orderMaster.advance', 'user'])
            ->join('order', 'order.order_id = m.order_id')
            ->order(['m.create_time' => 'desc'])
            ->group('order.order_id')
            ->paginate($query);
        foreach ($list as &$item) {
            if ($item['orderMaster']['order_source'] == 70) {
                $item['orderproduct']['total_pay_price'] = round($item['orderproduct']['total_pay_price'] + $item['orderMaster']['advance']['pay_price'], 2);
            }
        }
        return $list;
    }

    public function getList($query = [], $shop_supplier_id = 0)
    {
        $model = $this;
        // 查询条件：订单号
//        if (isset($query['order_no']) && !empty($query['order_no'])) {
//            $model = $model->where('order.order_no', 'like', "%{$query['order_no']}%");
//        }
        // 查询条件：起始日期
//        if (isset($query['create_time']) && !empty($query['create_time'])) {
//            $sta_time = array_shift($query['create_time']);
//            $end_time = array_pop($query['create_time']);
//            $model = $model->whereBetweenTime('m.create_time', $sta_time, date('Y-m-d 23:59:59', strtotime($end_time)));
//        }
//        // 售后类型
//        if (isset($query['type']) && $query['type'] > 0) {
//            $model = $model->where('m.type', '=', $query['type']);
//        }

        // 售后单状态(选项卡)
//        if (isset($query['status']) && $query['status'] >= 0) {
//            $model = $model->where('m.status', '=', $query['status']);
//        }
//        if ($shop_supplier_id) {
//            $model = $model->where('m.shop_supplier_id', '=', $shop_supplier_id);
//        }

        $where = [];
        if (!empty($query['order_no'])) {
            array_push($where, ['order_no', '=', $query['order_no']]);
        }
        if ($query['status'] == -1) {
            array_push($where, ['is_order_refund', '>=', 1]);
        } else if ($query['status'] == 1) {
            array_push($where, ['is_order_refund', '=', 1]);
        } else if ($query['status'] == 2) {
            array_push($where, ['is_order_refund', '=', 2]);
        } else if ($query['status'] == 3) {
            array_push($where, ['is_order_refund', '=', 3]);
        }
        unset($query['status']);
        array_push($where, ['shop_supplier_id', '=', $shop_supplier_id]);
        $params = $query;
        // 获取列表数据
        $list = \app\api\model\order\Order::listConditionNew($where, ['product','store','orderRefund'], $params);
        foreach ($list as $key => $value){
            $refund_audit_time = '';
            if($value['orderRefund']){
                foreach ($value['orderRefund'] as  $v){
                    $refund_audit_time = $v['audit_time']?date('Y-m-d H:i:s',$v['audit_time']):'';
                }
            }

            $list[$key]['refund_audit_time'] = $refund_audit_time;
        }
        return $list;
    }

    public function groupCount($query, $shop_supplier_id = 0)
    {
        $model = $this;
        // 查询条件：订单号
        if (isset($query['order_no']) && !empty($query['order_no'])) {
            $model = $model->where('order.order_no', 'like', "%{$query['order_no']}%");
        }
        // 查询条件：起始日期
        if (isset($query['create_time']) && !empty($query['create_time'])) {
            $sta_time = array_shift($query['create_time']);
            $end_time = array_pop($query['create_time']);
            $model = $model->whereBetweenTime('m.create_time', $sta_time, $end_time);
        }
        // 售后类型
        if (isset($query['type']) && $query['type'] > 0) {
            $model = $model->where('m.type', '=', $query['type']);
        }
        if ($shop_supplier_id) {
            $model = $model->where('m.shop_supplier_id', '=', $shop_supplier_id);
        }
        // 获取列表数据
        return $model->alias('m')
            ->field('m.status,COUNT(*) as total')
            ->join('order', 'order.order_id = m.order_id')
            ->group('m.order_id,m.status')->select()->toArray();
    }

    /**
     * 统计售后订单
     */
    public function getRefundOrderTotal($shop_supplier_id)
    {
        $filter['is_order_refund'] =1;
        $filter['shop_supplier_id'] = $shop_supplier_id;
        return (new OrderModel()) ->where($filter)->count();
    }

    /**
     * 已同意的退款
     */
    public function getRefundMoney($shop_supplier_id)
    {
        // 退款金额
        return $this->where('is_agree', '=', 10)
            ->where('shop_supplier_id', '=', $shop_supplier_id)
            ->sum('refund_money');
    }
}
